package org.dromara.system.controller;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Base64;
import java.util.Locale;
import java.util.UUID;

@RestController
@RequestMapping("/upload")
public class UploadController {


    /**
     * 上传方式2 MultipartFile 上传
     * @param file
     * @return
     * @throws IOException
     */
    @PostMapping("/method2")
    public String upload2(@RequestParam("file") MultipartFile file) throws IOException {
        String fileName = file.getOriginalFilename();
        String newName=UUID.randomUUID().toString()+fileName.substring(fileName.indexOf("."));
        // 获取当前操作系统
        String osName = System.getProperties().get("os.name").toString().toLowerCase(Locale.ROOT);
        String path="";

        if(osName.startsWith("win")){
            path="D:\\Test\\";
        }else{
            path="/mnt/test";
        }
        File saveFile=new File(path+newName);
        if(!saveFile.getParentFile().exists()){
            saveFile.getParentFile().mkdirs();
        }
        file.transferTo(saveFile);
        return saveFile.getPath();
    }



}

